class Solution {
public:
    int atoi(const char *str) {
        int ans=0;
        bool isNegtive=false;
        while (str && *str==' ') ++str;
        if (*str=='-') {
            isNegtive=true;
            ++str;
        } else
        if (*str=='+') ++str;
        while (str && '0'<=*str && *str<='9') {
            int digit=*str-'0';
            if (isNegtive) {
                if (ans<(INT_MIN+digit)/10) return INT_MIN;
                ans=ans*10-digit;
            } else {
                if (ans>(INT_MAX-digit)/10) return INT_MAX;
                ans=ans*10+digit;
            }
            str++;
        }
        return ans;
    }
};
